const baseUrl = import.meta.env.VITE_BASE_URL

const parameterValidation = (fromData, num) => {
  if (fromData.value.email == null && fromData.value.userSweetCircleId == null) {
    return { res: false, msg: '邮箱或用户id不能为空' }
  }

  if (fromData.value.email !== null) {
    // 验证邮箱格式
    if (!/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.com)/.test(fromData.value.email)) {
      return { res: false, msg: '邮箱格式不正确' }
    }
  }
  // 验证密码 密码不能空 长度为8-18位 必须包含 大小写字母和数字
  if (
    fromData.value.password.trim() === '' ||
    fromData.value.password.trim() === null ||
    fromData.value.password.trim() === undefined
  ) {
    return { res: false, msg: '确认密码不能为空' }
  }
  if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,18}$/.test(fromData.value.password)) {
    return { res: false, msg: '请输入长度在8-18位且包含字母和数字的确认密码' }
  }

  if (num !== 0) {
    if (num === 1) {
      // 验证 昵称 不能为空 长度为1-50位
      if (
        fromData.value.nickName.trim() === '' ||
        fromData.value.nickName.trim() === null ||
        fromData.value.nickName.trim() === undefined
      ) {
        return { res: false, msg: '昵称不能为空' }
      }
      if (fromData.value.nickName.length > 15) {
        return { res: false, msg: '昵称长度不可超过15位' }
      }
    }
    // 验证确认密码 不能为空 长度为8-18位 必须包含 大小写字母和数字 必须和原密码相同
    if (
      fromData.value.confirmPassword.trim() === '' ||
      fromData.value.confirmPassword.trim() === null ||
      fromData.value.confirmPassword.trim() === undefined
    ) {
      return { res: false, msg: '确认密码不能为空' }
    }
    if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,18}$/.test(fromData.value.confirmPassword)) {
      return { res: false, msg: '请输入长度在8-18位且包含字母和数字的确认密码' }
    }
    if (fromData.value.confirmPassword !== fromData.value.password) {
      return { res: false, msg: '两次密码不一致' }
    }
  }

  // 验证验证码
  if (
    fromData.value.captcha.trim() === '' ||
    fromData.value.captcha.trim() === null ||
    fromData.value.captcha.trim() === undefined
  ) {
    return { res: false, msg: '验证码不能为空' }
  }
  // 默认通过
  return { res: true, msg: '参数校验通过' }
}

/**
 * 根据参数  将 整数 计算为文件大小 kb mb gb
 * @params size
 * @return {string}
 */
const calculateSize = (size) => {
  if (size < 1024) {
    return size + 'B'
  } else if (size < 1024 * 1024) {
    return (size / 1024).toFixed(2) + 'KB'
  } else if (size < 1024 * 1024 * 1024) {
    return (size / (1024 * 1024)).toFixed(2) + 'MB'
  } else {
    return (size / (1024 * 1024 * 1024)).toFixed(2) + 'GB'
  }
}

const stringToBase64 = (str) => {
  return btoa(
    encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
      return String.fromCharCode('0x' + p1)
    })
  )
}
const base64ToString = (str) => {
  return decodeURIComponent(
    atob(str)
      .split('')
      .map(function (c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
      })
      .join('')
  )
}

const getAvatar = (userId, avatar, size) => {
  return `${baseUrl}/file/avatar?userId=${userId}&fileName=${avatar}&size=${size}`
}

const getStaticFileUrl = (name) => {
  return `${baseUrl}/static/file/${name}`
}

const playWarningTone = () => {
  new Audio('src/assets/audio/Windows Background.wav').play().then()
}
const playMessage = () => {
  new Audio('src/assets/audio/message.mp3').play().then()
}

const generateUUID = () => {
  return crypto.randomUUID().replace(/-/g, '').toUpperCase()
}

export default {
  baseUrl,
  parameterValidation,
  stringToBase64,
  base64ToString,
  getAvatar,
  playWarningTone,
  generateUUID,
  playMessage,
  getStaticFileUrl,
  calculateSize
}
